package com.robot.base.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.CursorLoader;
import androidx.loader.content.Loader;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建日期：2021/10/9  16:25
 * 类说明:
 *
 * @author：86152
 */
public class GalleryUtils {

    public static final int LOADER_ALL = 1;// 获取所有图片

    public static void initLoadManager(AppCompatActivity mContext, GalleryLoadListener loadListener) {
        List<PhotoItem> mDatas = new ArrayList<>();
        // loadManager.getInstance(this)
        // 注意点：getInstance上下文对象必须是AppCompatActivity才可以
        LoaderManager loaderManager = LoaderManager.getInstance(mContext);
        loaderManager.initLoader(LOADER_ALL, null, new LoaderManager.LoaderCallbacks<Cursor>() {
            private final String[] IMAGE_PROJECTION = {
                    MediaStore.Images.Media.DATA,
                    MediaStore.Images.Media.DISPLAY_NAME,
                    MediaStore.Images.Media.DATE_ADDED,
                    MediaStore.Images.Media._ID,
            };

            @NonNull
            @Override
            public Loader<Cursor> onCreateLoader(int i, @Nullable Bundle bundle) {
                if (i == LOADER_ALL) {
                    return new CursorLoader(mContext,
                            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                            IMAGE_PROJECTION, null, null, IMAGE_PROJECTION[2] + " DESC");
                }
                return null;
            }

            @Override
            public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor cursor) {
                if (cursor != null) {
                    mDatas.clear();
                    while (cursor.moveToNext()) {
                        PhotoItem item = new PhotoItem();
                        String path = cursor.getString(cursor.getColumnIndex(IMAGE_PROJECTION[0]));
                        String name = cursor.getString(cursor.getColumnIndex(IMAGE_PROJECTION[1]));
                        long date = cursor.getLong(cursor.getColumnIndex(IMAGE_PROJECTION[2]));
                        item.setCreateTime(date);
                        item.setName(name);
                        item.setPath(path);
                        mDatas.add(item);
                    }
                    loadListener.loadComplete(mDatas);
//                    mAdapter.setData(mDatas);
//                    mAdapter.notifyDataSetChanged();
                } else {
                    loadListener.loadFailed();
                }
            }

            @Override
            public void onLoaderReset(@NonNull Loader<Cursor> loader) {

            }
        });
    }

    private void initData(Context context) {
        // 获取SD卡的文件，有的比较耗时，一般我们不在Main-Thread线程中去做
        Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = resolver.query(uri, new String[]{
                MediaStore.Images.Media.DATA,
                MediaStore.Images.Media.DISPLAY_NAME,
                MediaStore.Images.Media.DATE_ADDED,
                MediaStore.Images.Media._ID}, null, null, null, null);
        while (cursor.moveToNext()) {
            PhotoItem photoItem = new PhotoItem();
            photoItem.setPath(cursor.getString(0));
            photoItem.setName(cursor.getString(1));
            photoItem.setCreateTime(cursor.getLong(2));
//            mDatas.add(photoItem);
        }
        cursor.close();
    }


    public interface GalleryLoadListener {

        void loadComplete(List<PhotoItem> datas);

        void loadFailed();
    }
}
